#!/usr/bin/env python3
from pathlib import Path
import json

EVAL_SCRIPT = Path("mini_dev/evaluation/run_evaluation.sh")
PRED_DIR = Path("mini_dev/sql_result")
REQUIRED_FILES = ["predict_mini_dev_mcts.json", "predict_mini_dev_evolutionary.json"]
DB_ROOT = Path("mini_dev/sqlite/dev_databases")
GROUND_TRUTH = Path("mini_dev/sqlite/mini_dev_sqlite_gold.sql")


def check():
    ok = True
    print("=> 检查 mini_dev 评估的可用性")
    if not EVAL_SCRIPT.exists():
        print(f"✗ 评估脚本缺失: {EVAL_SCRIPT}")
        ok = False
    else:
        print(f"✓ 找到评估脚本: {EVAL_SCRIPT}")

    if not PRED_DIR.exists():
        print(f"✗ 预测结果目录不存在: {PRED_DIR}")
        ok = False
    else:
        print(f"✓ 找到预测结果目录: {PRED_DIR}")

    for fname in REQUIRED_FILES:
        p = PRED_DIR / fname
        if not p.exists():
            print(f"✗ 预测文件缺失: {p}")
            ok = False
        else:
            try:
                with open(p, 'r', encoding='utf-8') as f:
                    data = json.load(f)
                print(f"✓ {fname} 格式 OK, {len(data)} 条预测")
            except Exception as e:
                print(f"✗ 无法读取 {p}: {e}")
                ok = False

    # Check dbs and ground-truth
    if not DB_ROOT.exists():
        print(f"✗ Mini-Dev sqlite databases missing at: {DB_ROOT}")
        ok = False
    else:
        print(f"✓ Found Mini-Dev sqlite databases directory: {DB_ROOT}")

    if not GROUND_TRUTH.exists():
        print(f"✗ Ground truth SQL file missing: {GROUND_TRUTH}")
        ok = False
    else:
        print(f"✓ Found ground-truth SQL file: {GROUND_TRUTH}")

    if ok:
        print("\n🎉 Mini-Dev 评估流程看起来已就绪（可运行）。")
        print("运行示例评估命令 (SQLite):")
        print("cd mini_dev/evaluation")
        print("./run_evaluation.sh\n    # 修改 predicted_sql_path 为 ../../mini_dev/sql_result/predict_mini_dev_mcts.json 或 predict_mini_dev_evolutionary.json")
    else:
        print("\n⚠️ 有问题需要修复：请按提示检查文件并下载缺失的评估脚本/文件")

    return ok


if __name__ == '__main__':
    import sys
    success = check()
    sys.exit(0 if success else 1)

    # Check dbs and ground-truth
    if not DB_ROOT.exists():
        print(f"✗ Mini-Dev sqlite databases missing at: {DB_ROOT}")
        ok = False
    else:
        print(f"✓ Found Mini-Dev sqlite databases directory: {DB_ROOT}")

    if not GROUND_TRUTH.exists():
        print(f"✗ Ground truth SQL file missing: {GROUND_TRUTH}")
        ok = False
    else:
        print(f"✓ Found ground-truth SQL file: {GROUND_TRUTH}")
